package com.example.controller;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Rule;
import com.example.entity.Student;
import com.example.mapper.StudentMapper;
import com.example.mapper.RuleMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/apply")

public class ApplicationController1 {
//研一入学即可获得奖学金，为了区分出等级，此处利用考研成绩代替学业成绩，保留学科竞赛和论文（这部分利用小数计算，更容易纵向对比不同学生的分数）
    @Resource
    StudentMapper studentMapper;
    @Resource
    RuleMapper ruleMapper;

    @PostMapping("/Grade1")
    public Map<String, String> save1(@RequestBody String info) { //接受JSON类数据

        JSONObject studentjsoninfo = JSONObject.parseObject(info);  // 将前端学生信息解析为一个JSONObject对象并返回

        String syear = studentjsoninfo.getString("年份");

        String student_grade = "研一";

        QueryWrapper<Rule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rule_grade", student_grade ).eq("rule_year", syear);


        Rule ruleyear = ruleMapper.selectOne(queryWrapper);  //取得对应年份的rule
        String rules = ruleyear.getRule_info();
        JSONObject rulejson = JSONObject.parseObject(rules);    //把写好的rule转换为JSON便于计算



        //学业成绩，此处即为研一的考研成绩
        JSONObject coursejson = (JSONObject) rulejson.get("学业成绩");
        int courseScore =studentjsoninfo.getIntValue("学业成绩");

        //此处考研成绩不做上限处理
        double studenttotal = courseScore;

        //科研能力同研二研三

        JSONObject sciencerule = (JSONObject) (rulejson.get("科研能力"));  // 规则的科研能力
        JSONObject sciencescore = (JSONObject) (studentjsoninfo.get("科研能力")); //学生的科研能力


        //学术论文部分
        JSONObject papervalue = (JSONObject) (sciencerule.get("学术论文")); //规则中的学术论文的分数 ，变量采用value
        double paper1value = Double.parseDouble(papervalue.get("CCF推荐A类国际学术期刊或中科院1区").toString());
        double paper2value = Double.parseDouble(papervalue.get("CCF推荐B类国际学术期刊或中科院2区").toString());
        double paper3value = Double.parseDouble(papervalue.get("CCF推荐C类国际学术期刊或中科院3区").toString());
        double paper4value = Double.parseDouble(papervalue.get("一般学术期刊 ").toString());

        double paper1 = 0 ;  // 赋初值为0
        double paper2 = 0 ;
        double paper3 = 0 ;
        double paper4 = 0 ;

        if (studentjsoninfo.get("CCF推荐A类国际学术期刊或中科院1区")!=null){
            paper1 = paper1 + Double.parseDouble(studentjsoninfo.get("CCF推荐A类国际学术期刊或中科院1区").toString());
        }
        if (studentjsoninfo.get("CCF推荐B类国际学术期刊或中科院2区")!=null){
            paper2 = paper2 + Double.parseDouble(studentjsoninfo.get("CCF推荐B类国际学术期刊或中科院2区").toString());
        }
        if (studentjsoninfo.get("CCF推荐C类国际学术期刊或中科院3区")!=null){
            paper3 = paper3 +Double.parseDouble(studentjsoninfo.get("CCF推荐C类国际学术期刊或中科院3区").toString());
        }
        if (studentjsoninfo.get("一般学术期刊 ")!=null){
            paper4 = paper4 +Double.parseDouble(studentjsoninfo.get("一般学术期刊").toString());
        }

        //学术论文分数 = 每篇论文分数相加
        double papertotal = paper1* paper1value + paper2*paper2value + paper3*paper3value + paper4*paper4value ;

        //学科竞赛部分（与论文申报类似）
        JSONObject pricevalue = (JSONObject) (sciencerule.get("学科竞赛"));
        double price1value = Double.parseDouble(pricevalue.get("国家级一等奖 ").toString());
        double price2value = Double.parseDouble(pricevalue.get("国家级二等奖 ").toString());
        double price3value = Double.parseDouble(pricevalue.get("国家级三等奖 ").toString());
        double price4value = Double.parseDouble(pricevalue.get("省级一等奖 ").toString());
        double price5value = Double.parseDouble(pricevalue.get("省级二等奖 ").toString());
        double price6value = Double.parseDouble(pricevalue.get("省级三等奖 ").toString());
        double price7value = Double.parseDouble(pricevalue.get("校赛奖项 ").toString());

        JSONObject pricescore =  sciencerule.getJSONObject("学科竞赛"); //学生信息处返回得到的学科竞赛

        double price1 = 0;
        double price2 = 0;
        double price3 = 0;
        double price4 = 0;
        double price5 = 0;
        double price6 = 0;
        double price7 = 0;

        if (studentjsoninfo.get("国家级一等奖")!=null){
            price1 =  price1 +Double.parseDouble(studentjsoninfo.get("国家级一等奖").toString());
        }
        if (studentjsoninfo.get("国家级二等奖")!=null){
            price2 =  price2 +Double.parseDouble(studentjsoninfo.get("国家级二等奖").toString());
        }
        if (studentjsoninfo.get("国家级三等奖")!=null){
            price3 =  price3 +Double.parseDouble(studentjsoninfo.get("国家级三等奖").toString());
        }
        if (studentjsoninfo.get("省级一等奖")!=null){
            price4 =  price4 +Double.parseDouble(studentjsoninfo.get("省级一等奖").toString());
        }
        if (studentjsoninfo.get("省级二等奖")!=null){
            price5 =  price5 +Double.parseDouble(studentjsoninfo.get("省级二等奖").toString());
        }
        if (studentjsoninfo.get("省级三等奖")!=null){
            price6 =  price6 +Double.parseDouble(studentjsoninfo.get("省级三等奖").toString());
        }
        if (studentjsoninfo.get("校赛奖项")!=null){
            price7 =  price7 +Double.parseDouble(studentjsoninfo.get("校赛奖项").toString());
        }

        double pricetotal = price1*price1value + price2*price2value + price3*price3value + price4*price4value + price5*price5value + price6*price6value + price7*price7value;

        double sciencetotal = papertotal + pricetotal ;

        sciencetotal = sciencetotal * 0.01; //这部分由于分值多为两位数，所以乘0.01


        //总分由整数的考研分数以及小数的科研能力组成，管理员可以通过两部分分数进行综合考量
        studenttotal = studenttotal +  sciencetotal;

        String student_id = studentjsoninfo.getString("学号");

        Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("student_id", student_id));

        //查询到学生后，自动把总分赋予该学生
        if (student != null) {
            student.setStudent_total(studenttotal);
            studentMapper.updateById(student);
        }

        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("申请状态", "success");

        return resultMap;

    }
}
